<?php

class Variable
{
	public $visibility;// 0 : public, 1 : protected, 2 : private
	public $name;
	
	function __construct($name)
	{
		$this->name = $name;
	}
}

class ClassContext
{
	public $listVariable;
	public $listMethod;
}

class FunctionContext
{
	public $name;
	public $argType;
	public $returnType;
	
	function __construct($tokenMngr, $tokenData)
	{
		$this->name = $tokenData->array[1]->text;
		if($tokenData->array[6]->token_id == $tokenMngr->getId('var_type'))
		{
			$var_type = $tokenData->array[6];
			$this->returnType = $var_type->array[0]->text;
		}
	}
}

function isMemVar($tokenMngr, $tokenData)
{
	$token = $tokenData->array[0];
	if($token == $tokenMngr->getId('string'))
		return true;
	$token2 = $tokenData->array[1];
	if($token == $tokenMngr->getId('('))
		return isMemVar($tokenMngr, $token2);
	if($token == $tokenMngr->getId('var') && ($token2 == $tokenMngr->getId('=') || $token2 == $tokenMngr->getId('+=') || $token2 == $tokenMngr->getId('-=') || $token2 == $tokenMngr->getId('*=') || $token2 == $tokenMngr->getId('/=')))
		return true;
	return false;
}

class CompilerPHP
{
	public $tokenMngr;
	public $listClass;
	public $listFunction;
	public $listVariable;
	
	function __construct($tokenMngr)
	{
		$this->tokenMngr = $tokenMngr;
		$this->listClass = array();
		$this->listFunction = array();
		$this->listVar = array();
	}
	
	public function checkClassAndFunction($tokenData)
	{
		$token = $this->tokenMngr->token_inv;
		if($tokenData->token_id == $token['list_instruction']))
			for($i = 0; $i < count($tokenData->array); $i++)
				$this->checkClassAndFunction($tokenMngr, $tokenData->array[$i]);
		if($tokenData->token_id == $token['instruction'])
		{
			$tokenData2 = $tokenData->array[0];
			if($tokenData2->token_id == $token['function'])
				$this->listFunction[] = new FunctionContext($tokenMngr, $tokenData2);
		}
	}
	
	public function compile($tokenData)
	{
		echo $tokenData->toString($this->tokenMngr);
		$this->checkClassAndFunction($tokenData);
		echo "<br/>";
		var_dump($this->listFunction);
		echo $this->compile_func($tokenData);
	}
	
	public function throwError($tokenData, $message)
	{
		echo $message."<br/>";
	}
	
	public function compile_func($tokenData)
	{
		$token = $this->tokenMngr->token_inv;
		$str = "";
		if($tokenData->token_id == $token['list_instruction'])
			for($i = 0; $i < count($tokenData->array); $i++)
				$str .= $this->compile_func($tokenData->array[$i]);
		else if($tokenData->token_id == $token['instruction'])
		{
			if($tokenData->array[0]->token_id == $token['var'])
				$str .= $this->compile_func($tokenData->array[0]).";\n";
			else if($tokenData->array[0]->token_id == $token['create var'])
			{
				$var = new Variable($tokenData->array[0]->array[1]->text);
				$this->listVariable[] = $var;
				if(count($tokenData->array)==4)
					$str .= '$'.$var->name.$tokenData->array[1]->text.$this->compile_func($tokenData->array[2]).";\n";
			}
			else if($tokenData->array[0]->token_id == $token['function'])
			{
				$listVariable = $this->listVariable;
				$this->listVariable = array();
				$array = $tokenData->array;
				if(count($array)==8)
					$str .= 'function '.$array[1]->text.'('.$this->compile_func($array[3]).'){'.$this->compile_func($array[6])."}\n";
				else $str .= 'function '.$array[1]->text.'('.$this->compile_func($array[3]).'){'.$this->compile_func($array[8])."}\n";
				$this->listVariable = $listVariable;
			}
		}
		else if($tokenData->token_id == $token['mem_var'])
		{
			$array = $tokenData->array;
			if($array[0]->token_id == $token['string'])
				$str .= '$'.$array[0]->text;
			else if($array[0]->token_id == $token['('])
				$str .= '('.$this->compile_func($array[1]).')';
			else if($array[0]->token_id == $token['mem_var'])
				$str .= $this->compile_func($array[0]).$array[1]->text.$this->compile_func($array[2]);
		}
		else if($tokenData->token_id == $token['var'])
		{
			$array = $tokenData->array;
			if(count($array) == 1)
				$str .= $this->compile_func($array[0]);
			else if(count($array) == 2)
			{
				if($array[0] == $token['prefix -'])
				{
					if($array[1]->array[0]->token_id == $token['function_anonymous'])
						$this->throwError($tokenData, 'syntax error : \'-\' devant une fonction');
					$str .= '-'.$this->compile_func($array[1]);
				}
				else if($array[0] == $token['++'] || $array[0] == $token['--'] || $array[0] == $token['!'])
					$str .= $array[0]->text.$this->compile_func($tokenData->array[1]);
				else if($array[1] == $token['++'] || $array[1] == $token['--'])
					$str .= $this->compile_func($tokenData->array[0]).$array[1]->text;
			}
			else if(count($array) == 3)
				$str .= $this->compile_func($array[0]).$array[1]->text.$this->compile_func($array[2]);
			else if(count($array) == 5)
				$str .= $this->compile_func($array[0]).$array[1]->text.$this->compile_func($array[2]).$array[3]->text.$this->compile_func($array[4]);
		}
		else if($tokenData->token_id == $token['number'])
			$str .= $tokenData->text;
		else $str.='error';
		return $str;
	}
}
?>